﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Expo.Training.DataAccess.Activity;

public partial class Activity_ActivityManagement : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            cc_ExPager.PageIndex = 0;
            Bind();
        }

    }

    private void Bind()
    {
        cc_ExPager.PageSize = 100;
        List<ActivityEntity> _list = ActivityModule.GetAll();

        this.rpt_ActivityList.DataSource = _list;
        this.rpt_ActivityList.DataBind();
    }

    protected void PageIndex_Changed(object sender, EventArgs e)
    {
        Bind();
    }

    protected void rpt_Activity_ItemCommand(object sender, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "delete")
        {
            Guid _id = new Guid(e.CommandArgument.ToString());

            try
            {
                string _condition = "a.ActivityID = '" + _id.ToString() + "'";


                if (ActivityModule.GetRegistrations(_condition).Count > 0)
                {
                    this.AddEndScript("error", "alert('活动已有报名用户，无法删除！');");
                }
                else
                {
                    if (ActivityModule.Delete(_id))
                    {
                        this.AddEndScript("delete succes", "alert('删除活动成功！');location.href='ActivityManagement.aspx'");
                    }
                    else
                    {
                        this.AddEndScript("error", "alert('删除操作出错，请重试！');");
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            Bind();
        }
    }

    protected void rpt_Activity_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
             Literal rowIndex = (Literal)e.Item.FindControl("rowIndex");
             rowIndex.Text = (e.Item.ItemIndex + 1).ToString();
         }
    }
}
